﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarriorRush.GameDynamics.Utilities;
using WarriorRush.GameDynamics;
using WarriorRush.GameDynamics.Characters;
using WarriorRush;
using Microsoft.Xna.Framework;

namespace WarriorRush.UserInterface
{
    public class SelectionHandler
    {
        public List<Selectable> Squares;
        public HighlightType SelectType;
        public int MaxSelections;
        public int TotalSelected;
        public bool IsReplacedSelection;

        public Selectable FindSelectable(Point tapPoint)
        {
            return Squares.Where(f => f.Square.Box.Contains( tapPoint)).FirstOrDefault() ;
        }

        public bool HandleSelection(Point tapPoint)
        {
            var selection = Squares.Where(f => f.Square.Box.Contains(tapPoint)).FirstOrDefault();
            if (selection != null)
            {
                if (selection.IsSelected)
                {
                    if (!IsReplacedSelection)
                    {
                        selection.IsSelected = false;
                        TotalSelected--;
                    }
                }
                else if (TotalSelected < MaxSelections || IsReplacedSelection)
                {
                    if (IsReplacedSelection && MaxSelections == 1)
                        SetAll(false);
                    selection.IsSelected = true;
                    TotalSelected++;
                    
                }
                return true;      
            }
            return false;    
        }

        public void SetAll(bool IsSeleted)
        {
            foreach (var selection in Squares)
                selection.IsSelected = IsSeleted;
        }

        public List<ArenaSquare> GetSelectedSquares()
        {

            if (Squares != null)
                return Squares.Where(f => f.IsSelected).Select(f => f.Square).ToList();
            else
                return new List<ArenaSquare>();
        }
 
    }
}
